package com.sinoglobal.sinostore.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;

import com.sinoglobal.sinostore.system.Constants;

/**
 * 
 * @author ty
 * @createdate 2012-6-14 下午6:09:16
 * @Description: 时间工具类
 */
@SuppressLint("SimpleDateFormat")
public class TimeUtil {
	public static final DateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
	// public static final DateFormat sdf2 = new
	// SimpleDateFormat("yyyy年MM月dd日");
	// public static final DateFormat sdf3=new
	// SimpleDateFormat("yyyy年MM月dd日 E"); //E表示星期几
	// public static final DateFormat sdf4=new
	// SimpleDateFormat("yyyy-MM-dd HH"); //精确到小时
	// private static final DateFormat dfYear = new SimpleDateFormat("yyyy");
	// private static final DateFormat dfDay = new SimpleDateFormat("dd");
	// private static final DateFormat dfMonth = new
	
	// SimpleDateFormat("MM-dd HH:mm");
	public static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	public static final DateFormat dfHour = new SimpleDateFormat("HH:mm");
	private static final String SUNDAY = "周日";
	private static final String SUNDAYS = "星期日";
	private static final String MONDAY = "周一";
	private static final String MONDAYS = "星期一";
	private static final String TUESDAY = "周二";
	private static final String TUESDAYS = "星期二";
	private static final String WEDNESDAY = "周三";
	private static final String WEDNESDAYS = "星期三";
	private static final String THURSDAY = "周四";
	private static final String THURSDAYS = "星期四";
	private static final String FRIDAY = "周五";
	private static final String FRIDAYS = "星期五";
	private static final String SATURDAY = "周六";
	private static final String SATURDAYS = "星期六";
	private static final long DATA_TIME = 1000 * 60 * 60 * 24;

	/**
	 * 
	 * @author ty
	 * @createdate 2012-6-17 下午1:53:04
	 * @Description: 将字符串根据日期的格式进行格式化
	 * @param df
	 * @param date
	 * @return
	 */
	public static Date parseDate(DateFormat df, String date) {
		try {
			return df.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @author ty
	 * @createdate 2012-9-23 下午3:24:34
	 * @Description: 将Long值格式化
	 * @param df
	 * @param date
	 * @return
	 */
	public static String parseDateToString(DateFormat df, Date date) {
		return df.format(date);
	}

	

	/**
	 * @author ty
	 * @createdate 2012-9-22 下午4:47:08
	 * @Description: 获取星期几
	 * @param weekNum
	 * @return
	 */
	public static String[] getWeek(int weekNum) {
		String[] week = null;
		switch (weekNum) {
		case 0:
			week = new String[] { SUNDAY, SUNDAYS };
			break;
		case 1:
			week = new String[] { MONDAY, MONDAYS };
			break;
		case 2:
			week = new String[] { TUESDAY, TUESDAYS };
			break;
		case 3:
			week = new String[] { WEDNESDAY, WEDNESDAYS };
			break;
		case 4:
			week = new String[] { THURSDAY, THURSDAYS };
			break;
		case 5:
			week = new String[] { FRIDAY, FRIDAYS };
			break;
		case 6:
			week = new String[] { SATURDAY, SATURDAYS };
			break;
		}
		return week;
	}

	private static String getWeekDay(int weekNum) {
		String week = null;
		switch (weekNum) {
		case 0:
			week = SUNDAYS;
			break;
		case 1:
			week = MONDAYS;
			break;
		case 2:
			week = TUESDAYS;
			break;
		case 3:
			week = WEDNESDAYS;
			break;
		case 4:
			week = THURSDAYS;
			break;
		case 5:
			week = FRIDAYS;
			break;
		case 6:
			week = SATURDAYS;
			break;
		}
		return week;
	}
	
	private static String getWeekDay1(int weekNum) {
		String week = null;
		switch (weekNum) {
		case 0:
			week = SUNDAY;
			break;
		case 1:
			week = MONDAY;
			break;
		case 2:
			week = TUESDAY;
			break;
		case 3:
			week = WEDNESDAY;
			break;
		case 4:
			week = THURSDAY;
			break;
		case 5:
			week = FRIDAY;
			break;
		case 6:
			week = SATURDAY;
			break;
		}
		return week;
	}

	/**
	 * @author ty
	 * @createdate 2012-9-28 下午3:42:07
	 * @Description: 获得日期的星期数
	 * @param dt
	 * @return
	 */
	public static String getWeekOfDate(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return getWeekDay(w);
	}

	/**
	 * @author bo.wang
	 * @createdate 2012-10-9 下午4:46:59
	 * @Description 获取几天前的Date
	 * @param d
	 *            传递进来的Date
	 * @param day
	 *            第几天
	 */
	public static Date getDateBefore(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
		return now.getTime();
	}

	/**
	 * 
	 * @author bo.wang
	 * @createdate 2012-10-9 下午4:47:14
	 * @Description 获取几天后的Date
	 * @param day
	 *            第几天
	 */
	public static Date getDateAfter(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return now.getTime();
	}

	/**
	 * 
	 * @author bo.wang
	 * @createdate 2012-10-26 下午2:48:04
	 * @Description 将string转换成Date，String格式为“2012-10-26”
	 * @param dateString
	 *            需要转换的字符串
	 * @return 返回转换完成的Date，如果转换失败，返回null
	 */
	public static Date parseStringToDate(String dateString) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = sdf.parse(dateString);
			return date;
		} catch (ParseException e) {
			System.out.println(e.getMessage());
		}

		return null;
	}

	public static Date parseStringToDate1(String dateString) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日  HH:mm");
			Date date = sdf.parse(dateString);
			return date;
		} catch (ParseException e) {
			System.out.println(e.getMessage());
		}

		return null;
	}

	/**
	 * 
	 * @author czz
	 * @createdate 2014-1-13 下午3:50:46
	 * @Description: (用一句话描述该方法做什么)
	 * @param datestring
	 * @return
	 * 
	 */
	public static String praseStringtoString(String datestring) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
			Long l = Long.parseLong(datestring) * 1000;
			Date date = new Date(l);
			return sdf.format(date);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return datestring;
		}
	}

	/**
	 * @author ty
	 * @createdate 2013-4-26 下午3:51:48
	 * @Description: date转换时间戳
	 * @return
	 * @throws ParseException
	 */
	public static long getTimestamp(Date date1) throws ParseException {
		Date date2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("1970/01/01 08:00:00");
		long l = date1.getTime() - date2.getTime() > 0 ? date1.getTime() - date2.getTime() : date2.getTime() - date1.getTime();
		long rand = (int) (Math.random() * 1000);
		return rand;
	}
	public static long getTimestamp1(Date date1) throws ParseException {
		Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("1970-01-01 08:00:00");
		long l = date1.getTime() - date2.getTime() > 0 ? date1.getTime() - date2.getTime() : date2.getTime() - date1.getTime();
		return l;
	}

	/**
	 * @author miaoxin.ye
	 * @createdate 2012-10-31 下午7:46:42
	 * @Description: 将字符串转化为日期
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date parseStringToDate(String dateStr, DateFormat df) {
		try {
			Date date = df.parse(dateStr);
			return date;
		} catch (ParseException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	/**
	 * 
	 * @author bo.wang
	 * @createdate 2012-10-26 下午3:56:55
	 * @Description 将time转换成long型
	 * @param df
	 *            格式化对象
	 * @param time
	 *            需要转换的字符串
	 * @param 返回long型的值
	 */
	public static long parseStringToLong(DateFormat df, String time) {
		try {
			return df.parse(time).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0L;
	}


	/* 时间戳转换成字符窜 */
	public static String getDateToString(long time) {
		Date d = new Date(time);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sf.format(d);
	}

	/* 时间戳转换成字符窜 */
	public static String getDateToString1(long time) {
		Date d = new Date(time);
		SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
		return sf.format(d);
	}

	/* 时间戳转换成字符窜 */
	public static String getDateToString2(long time) {
		Date d = new Date(time);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		return sf.format(d);
	}

	public static String parseTimestampToDateStrForMill(String create_time) {
		long time = 0;
		Date date = new Date();
		try {
			time = Long.parseLong(create_time);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return create_time;
		}
		date.setTime(time);

		return parseDateToString(date);
		// return getDateSpace(currentDate, date);
	}

	public static String parseDateToString(Date date) {
		return df.format(date);
	}

	/**
	 * @author sbk
	 * @createdate 2014-8-15 下午7:20:29
	 * @Description: 返回毫秒时间差
	 * @param time1
	 * @param time2
	 * @return
	 * 
	 */
	public static long getTimeSpace(String time1, String time2) {
		long result = 0;
		try {
			result = Long.parseLong(time2) - Long.parseLong(time1);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}

	public static int getYear() {
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	public static String parseTime(String time) {
		long timeStemp = 0;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date;
		try {
			date = simpleDateFormat.parse(time);
			timeStemp = date.getTime();

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return String.valueOf(timeStemp);

	}

	public static Date stringToDate(String dateString) {
		ParsePosition position = new ParsePosition(0);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date dateValue = simpleDateFormat.parse(dateString, position);
		return dateValue;
	}

	/**
	 * 
	 * @author zbl
	 * @methods getTime
	 * @description 列表时间显示
	 * @date 2014-12-18 下午9:09:59
	 * @param datetime
	 * @return 参数说明
	 */
	public static String getTime(String datetime) {

		String message = "";
		try {
			// 传过来的时间
			// Date dates = sdf.parse("2014-12-14 13:10:10");
			// datetime = String.valueOf(getTimestamp(dates));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			// 当前系统时间
			long time = System.currentTimeMillis();
			// 得到当前是星期几
			int todayWeek = getWeekdayOfDateTime(String.valueOf(time));

			// 当前天的凌晨时间戳
			String nowTimeString = parseTimeStampToDateString2(time);
			Date date = sdf.parse(nowTimeString + " 00:00:00");
			long todayTime = getTimestamp1(date);

			long lTime = todayTime - Long.parseLong(datetime);

			int x = (int) (lTime / DATA_TIME) + 1;
			if (lTime < 0) {

				message = getDateToString1(Long.parseLong(datetime));
			} else if (x < todayWeek) {

				int week = todayWeek - x;
				message = getWeekDay1(week);

			} else {
				Calendar newCalendar = Calendar.getInstance();
				int newYear = newCalendar.get(Calendar.YEAR);// 系统当前的年份
				Calendar calendar = Calendar.getInstance();
				calendar.setTimeInMillis(Long.parseLong(datetime));
				int year = calendar.get(Calendar.YEAR);// 用户选择的年份
				if (year == newYear) {
					message = parseTimeStampToDateString(datetime).substring(5);
				} else {
					message = parseTimeStampToDateString(datetime);

				}
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return message;
	}

	public static String parseTimeStampToDateString2(long timeStamp) {
		try {
			Date date = new Date(timeStamp);
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			return parseDateToString(df, date);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "";
	}

	public static int getWeekdayOfDateTime(String datetime) {
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		int weekday = c.get(Calendar.DAY_OF_WEEK) - 1;
		return weekday;
	}

	public static String parseTimeStampToDateString(String timeStamp) {
		try {
			Date date = new Date(Long.parseLong(timeStamp));
			DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
			return parseDateToString(df, date);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return timeStamp;
	}

}
